<%@ page import="com.ychs.entity.College" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<div class="layuimini-container">
    <div class="layuimini-main">
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加院系</button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 批量删除
                </button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>


    </div>
</div>
<script>
    layui.use(['form', 'table','miniPage','element'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            layer = layui.layer,
            miniPage = layui.miniPage;

        form.render();
        table.render({
            elem: '#currentTableId',
            url: 'CollegeServlet?method=search',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox", width: 80},
                {field: 'name', width: 280, title: '院系名称', sort: true},
                {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}

            ]],
            limits: [15,30,45,60,75,90],
            limit: 15,
            page: true,
            skin: 'line'
        });



        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var result = JSON.stringify(data.field);
            layer.alert(result, {
                title: '最终的搜索信息'
            });

            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: {
                    searchParams: result
                }
            }, 'data');

            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {

            var content = miniPage.getHrefContent('page/college/collegeAdd.jsp');
            var openWH = miniPage.getOpenWidthHeight();
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加院系',
                    type: 1,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: [openWH[0] + 'px', openWH[1] + 'px'],
                    offset: [openWH[2] + 'px', openWH[3] + 'px'],
                    content: content,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                if (data.length == 0) {
                    layer.alert("请选择要删除的记录",{icon:0});
                }else{
                    layer.confirm("确认删除这些信息吗？",function (index) {

                        $.ajaxSettings.async=false;
                        //删除成功的条数
                        let successNumber = 0;
                        for(let i=0;i<data.length;i++){
                            let url = 'CollegeServlet';
                            let params = {method:'remove',id:data[i].id};
                            let fn = function (res) {
                                if(res=='true'){
                                    successNumber++;
                                }
                            };
                            $.post(url,params,fn);
                        }
                        $.ajaxSettings.async=true;
                        table.reload("currentTableId");
                        layer.msg("成功删除[" + successNumber + "]条记录", {icon: 1});
                        layer.close(index);
                    })
                }
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {

                let id = data.id;
                let url = 'CollegeServlet?method=searchById&id='+id;
                let name,code;
                $.get(url,function (res) {

                    if (res == 'null') {
                        layer.msg("记录不存在，请刷新",{icon: 1});
                        return false;
                    }

                    //res将来返回的是college的字符串形式
                    res = JSON.parse(res);
                    name = res.name;
                    code = res.code;


                    var content = miniPage.getHrefContent('page/college/collegeModify.jsp?id='+id+'&name='+name+'&code='+code);
                    var openWH = miniPage.getOpenWidthHeight();

                    var index = layer.open({
                        title: '编辑院系',
                        type: 1,
                        shade: 0.2,
                        maxmin: true,
                        shadeClose: true,
                        area: [openWH[0] + 'px', openWH[1] + 'px'],
                        offset: [openWH[2] + 'px', openWH[3] + 'px'],
                        content: content,
                    });
                    $(window).on("resize", function () {
                        layer.full(index);
                    });
                    return false;
                })


            } else if (obj.event === 'delete') {
                layer.confirm('真的删除行么', function (index) {
                    let url = 'CollegeServlet';
                    let params = {method:'remove', id: data.id};
                    let fn = function(res){
                        //后台删除成功
                        if (res == 'true') {
                            obj.del();
                            layer.msg("删除成功",{icon:1});
                        }else{
                            layer.msg("删除失败",{icon:2});
                        }
                    }
                    //ajax
                    $.post(url,params,fn);

                    layer.close(index);
                });
            }
        });

    });
</script>

</body>
</html>